package com.qfedu;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qfedu.domain.Clazz;
import com.qfedu.domain.Student;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 创建者: QF-SUN
 * 创建时间: 2023/3/30 14:13
 * 描述:  jackson 转换json
 */
public class TestJSONNJackson {

    public static void main(String[] args) throws IOException {
//        Clazz clazz = new Clazz(1,"2301");
//        Student stu = new Student(1, "李志", 18, "中国北科", clazz );
//
//        //把对象转换json
//        ObjectMapper objectMapper = new ObjectMapper();
//        String jsonObject = objectMapper.writeValueAsString(stu);
//
//
//        System.out.println(jsonObject);
//
//        //把json串转换为 java对象
//        Student s = objectMapper.readValue(jsonObject, Student.class);
//        System.out.println(s);
//
//
//        List<Student> list = new ArrayList<>();
//
//        list.add(new Student(1, "李志", 18, "中国北科", clazz));
//        list.add(new Student(2, "曹雷", 18, "中国北科", clazz));
//        list.add(new Student(3, "郑家书", 18, "中国北科", clazz));
//        list.add(new Student(4, "龚博宇", 18, "中国北科", clazz));
//
//        String str2 = objectMapper.writeValueAsString(list);
//        System.out.println(str2);
//
//        ArrayList list1 = objectMapper.readValue(str2, new TypeReference<ArrayList>() {
//        });
//
//        System.out.println(list1);


        String string = "{\"id\":1,\"name\":\"李志\",\"age\":18,\"address\":\"中国北科\",\"clazz\":{\"id\":1,\"name\":\"2301\"}}";
        Map map = JSON.parseObject(string, Map.class);
        System.out.println(map.get("address"));
        System.out.println(map.get("clazz"));

    }
}
